﻿using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Xml;

using Microsoft.Win32;

using Aulac.Srv.Db.Connect; using Aulac.Srv.Db.Common;
using System.Collections.Generic;
using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using Aulac.Srv.Db.Data.Catalogs;
using Aulac.Rsc.Bss.Tags;
using Aulac.Rsc.Bss;
using Aulac.Srv.Data.XmlQuery;



namespace Aulac.Srv.Db.Data.Ems.Items
{
    public partial class MdnGrpItems : Aulac.Srv.Db.Data.Ems.Document.DbEmsDataDocumentMangager
	{
        public MdnGrpItems(IDbAulacConnect p_Db)
            : base(p_Db)
        {
        }
        List<string> mList = new List<string>(new string[] { tagItems.ItemGrps.ItemGrpPrkID, tagItems.ItemGrps.ItemGrpID, tagItems.ItemGrps.ItemGrpName });


        public XmlDataDocument GetListMdnItemsGrp(XmlDataDocument xmlData)
        {
            try
            {
                string[] sArrItemGrpPrkID = XmlProcess.GetValueParam(xmlData, tagItems.MdnGrpItems.ItemGrpPrkID).Split(',');
                string sItemGrpPrkID = "";
                for (int i = 0; i < sArrItemGrpPrkID.Length; i++)
                {
                    sItemGrpPrkID += string.Format("{0},", GetListMdnGrpPrkID(GetItemGrpID(sArrItemGrpPrkID[i])));
                }
                sItemGrpPrkID = sItemGrpPrkID.TrimEnd(',');
                XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                
                sqlQuery = string.Format("SELECT {0} FROM {1}", Aulac.Global.Common.StringHelper.ConvertArrayToString(",", mList.ToArray()), TableNames.Dm_MdnGrpItems);
                sqlQuery += string.Format(" WHERE (@{0} < 1 OR {0} >= @{0} )", tagItems.MdnGrpItems.IsActive);
                sqlQuery += string.Format(" AND ({0} IS NULL OR ({0} IN ({1})))", tagItems.MdnGrpItems.ItemGrpPrkID,sItemGrpPrkID);

                SrvExecXmlQueryData query = new SrvExecXmlQueryData(DbDataProvider);
                query.SetVchLinesElm(QueryCmdTypes.SelectData);
                query.ObjQueryVchLines.SqlQueryString = sqlQuery;
                query.ObjQueryVchLines.AddValueParamFilterItem(tagItems.MdnGrpItems.IsActive, tagItems.MdnGrpItems.IsActive, true); 
                return query.ExecureQuery();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        public XmlDataDocument GetListMdnItemGrp(XmlDataDocument xmlData)
        {
            try
            {
                string[] sArr = new string[] { tagItems.MdnGrpItems.CapAssGrpPrkID, tagItems.MdnGrpItems.ItemGrpID, tagItems.MdnGrpItems.ItemGrpName };
                XmlDataDocument xmlDoc = CreateXmlDocument("", "");
                string sqlQuery = string.Format("SELECT	{0}", StringHelper.ConvertArrayToString(", ", sArr));
                sqlQuery += string.Format("  FROM {0}", TableNames.Dm_MdnGrpItems);

                SrvExecXmlQueryData query = new SrvExecXmlQueryData(DbDataProvider);
                query.SetVchLinesElm(QueryCmdTypes.SelectData);
                query.ObjQueryVchLines.SqlQueryString = sqlQuery;
                return query.ExecureQuery();
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        #region Private Methods
        private string GetItemGrpID(string pMdnGrpPrkID)
        {
            sqlQuery = SqlHelper.CreateSqlSelect(TableNames.Dm_MdnGrpItems, tagItems.MdnGrpItems.ItemGrpID);
            sqlQuery += string.Format(" WHERE {0} = ? ", tagItems.MdnGrpItems.ItemGrpPrkID);
            return DbDataProvider.SelectValueFromDb(sqlQuery, pMdnGrpPrkID);
        }

        private string GetListMdnGrpPrkID(string pMdnGrpID)
        {
            try
            {
                string ret = "";
                sqlQuery = SqlHelper.CreateSqlSelect(TableNames.Dm_MdnGrpItems, tagItems.MdnGrpItems.ItemGrpPrkID);
                sqlQuery += string.Format(" WHERE SUBSTRING({0},1, {1}) = ?", tagItems.MdnGrpItems.ItemGrpID, pMdnGrpID.Length);
                DataTable db = new DataTable();
                DbDataProvider.ExecuteReaderDataToTable(mDbCmd, sqlQuery, ref db, pMdnGrpID);
                for (int i = 0; i < db.Rows.Count; i++)
                {
                    ret += string.Format("'{0}',", DataTableProcess.GetValue(db.Rows[i], tagItems.MdnGrpItems.ItemGrpPrkID));
                }
                return ret.TrimEnd(',');
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
        #endregion
    }
}
